package www.woniuxy.com.class112.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import www.woniuxy.com.class112.dao.IssueTypeMapper;
import www.woniuxy.com.class112.entity.IssueType;
import www.woniuxy.com.class112.entity.IssueTypeExample;
import www.woniuxy.com.class112.param.issues.SelectIssueTypeParam;
import www.woniuxy.com.class112.service.IssueTypeService;

import javax.annotation.Resource;
import java.util.List;

@Service
public class IssueTypeServiceImpl implements IssueTypeService {
    @Resource
    private IssueTypeMapper issueTypeMapper;
    @Override
    public List<IssueType> queryQuestionCategory() {
        return issueTypeMapper.all();
    }

    @Override
    public PageInfo<IssueType> queryQuestionCategoryByname(SelectIssueTypeParam param) {
        PageHelper.startPage(param.getPageIndex() == null ? 1 : param.getPageIndex(), param.getPageSize() == null ? 7 : param.getPageSize());
        IssueTypeExample example = new IssueTypeExample();
        IssueTypeExample.Criteria criteria = example.createCriteria();
        if (param.getName()!=null){
            criteria.andIssueNameLike("%" + param.getName()+"%");
        }
        return new PageInfo<>(issueTypeMapper.selectByExample(example));
    }

    @Override
    public void addIssueType(String issueName) {
        IssueTypeExample example = new IssueTypeExample();
        IssueTypeExample.Criteria criteria = example.createCriteria();
        criteria.andIssueNameEqualTo(issueName);
        List<IssueType> issueTypes = issueTypeMapper.selectByExample(example);
        if (issueTypes.size()>0){
            throw new RuntimeException("该问题类别已存在");
        }
        IssueType issueType = new IssueType();
        issueType.setIssueName(issueName);
        issueTypeMapper.insert(issueType);
    }
}
